<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			$(function(){
				let selects = $('#city>select');
				//把后三个禁用
				for(let i = 1 ; i < selects.length ; i++){
					selects[i].disabled = true;
				}
				$.ajax({ 
					type: 'post', //可以用GET,也可以用POST...
					url: 'http://localhost/city_4j/php_josn_ajax.php?jsoncallback=?', 
					dataType: 'jsonp', 
					jsonp: "jsoncallback", 
					success: function(data) { 
						$.each(data,function(k,v){
							selects[0].append(new Option(v,k));
						});
					}
				});
				
				
				//事件方法的代码结构相似,可以优化.我就不做了
				//一级城市下拉选中事件
				$(selects[0]).change(function(){
					selects[1].options.length = 1;
					selects[2].options.length = 1;
					selects[3].options.length = 1;
					selects[2].disabled = true;
					selects[3].disabled = true;
					//填充二级城市列表
					$.ajax({
						type: 'post', //可以用GET,也可以用POST...
						url: 'http://localhost/city_4j/php_josn_ajax.php?jsoncallback=?', 
						data: {"code": this.value},
						dataType: 'jsonp', 
						jsonp: "jsoncallback", 
						success: function(data) { 
							$.each(data,function(k,v){
								selects[1].append(new Option(v,k));
							});
							selects[1].disabled = false;
						}
					});
					
				});
				//二级城市下拉选中事件
				$(selects[1]).change(function(){
					selects[2].options.length = 1;
					selects[3].options.length = 1;
					selects[3].disabled = true;
					//填充三级城市列表
					$.ajax({
						type: 'post', //可以用GET,也可以用POST...
						url: 'http://localhost/city_4j/php_josn_ajax.php?jsoncallback=?', 
						data: {"code": this.value},
						dataType: 'jsonp', 
						jsonp: "jsoncallback", 
						success: function(data) { 
							$.each(data,function(k,v){
								selects[2].append(new Option(v,k));
							});
							selects[2].disabled = false;
						}
					});
				});
				//三级城市下拉选中事件
				$(selects[2]).change(function(){
					selects[3].options.length = 1;
					selects[3].disabled = false;
					//填充三级城市列表
					$.ajax({
						type: 'post', //可以用GET,也可以用POST...
						url: 'http://localhost/city_4j/php_josn_ajax.php?jsoncallback=?', 
						data: {"code": this.value},
						dataType: 'jsonp', 
						jsonp: "jsoncallback", 
						success: function(data) { 
							$.each(data,function(k,v){
								selects[3].append(new Option(v,k));
							});
							selects[3].disabled = false;
						}
					});
				});
				
			});
			
		</script>
	</head>
	<body>
		<form id="city" action="" method="">
			<select name="city1">
				<option value="">请选择</option>
			</select>
			<select name="city2">
				<option value="">请选择</option>
			</select>
			<select name="city3">
				<option value="">请选择</option>
			</select>
			<select name="city4">
				<option value="">请选择</option>
			</select>
		</form>
	</body>
</html>
